<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserDayData extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->order('id','desc')->find();
    }

    /**
     * 添加记录
     * @param $userCount
     * @param $orderInfo
     * @return array|bool
     */
    function addData($userCount,$orderInfo){
        $addData = [
            'user_id'=>$userCount['order_user_id'],
            'day_month'=>date("Y-m",strtotime($orderInfo['pay_time'])),
            'day_data'=>date("Y-m-d",strtotime($orderInfo['pay_time'])),
            'charge_electricity'=>0,
            'charge_money'=>0,
            'charge_number'=>0,
            'charge_time'=>0,
            'service_cost'=>0,
            'charge_cost'=>0,
            'carbon_dioxide_number'=>0,
            'tree_planting_number'=>0,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
            'real_charge_electricity'=>0,
            'real_charge_money'=>0,
            'real_charge_number'=>0,
            'real_charge_time'=>0,
            'real_service_cost'=>0,
            'real_charge_cost'=>0,
            'time_order'=>strtotime(date("Y-m-d",strtotime($orderInfo['pay_time']))),
        ];
        $addResult = $this->insertGetId($addData);
        if($addResult){
            $addData['id'] = $addResult;
            return $addData;
        }else{
            return false;
        }
    }

    /**
     * 添加记录
     * @param $userId
     * @param $getDay
     * @return array|bool
     */
    function addUserData($userId,$getDay){
        $addData = [
            'user_id'=>$userId,
            'day_month'=>date("Y-m",strtotime($getDay)),
            'day_data'=>$getDay,
            'charge_electricity'=>0,
            'charge_money'=>0,
            'charge_number'=>0,
            'charge_time'=>0,
            'service_cost'=>0,
            'charge_cost'=>0,
            'carbon_dioxide_number'=>0,
            'tree_planting_number'=>0,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
            'real_charge_electricity'=>0,
            'real_charge_money'=>0,
            'real_charge_number'=>0,
            'real_charge_time'=>0,
            'real_service_cost'=>0,
            'real_charge_cost'=>0,
            'time_order'=>strtotime(date("Y-m-d",strtotime($getDay))),
        ];
        $addResult = $this->insertGetId($addData);
        if($addResult){
            $addData['id'] = $addResult;
            return $addData;
        }else{
            return false;
        }
    }

    /**
     * 更新记录
     * @param $id
     * @param $saveData
     * @return static
     */
    function saveData($id,$saveData){
        return $this->where('id',$id)->update($saveData);
    }

    /**
     * 获取用户奖励列表
     * @param $where
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserDayList($where,$page = 1,$limit = 10){
        $count = $this->where($where)->count();
        $list = $this->where($where)->page($page,$limit)->order('time_order','desc')->select();
        if($list) $list = $list->toArray();
        return compact('count','list');
    }

}
